package org.lds.hotkey.console.business.hotkey.converter;

import org.lds.hotkey.console.business.app.entity.App;
import org.lds.hotkey.console.business.app.mapper.AppMapper;
import org.lds.hotkey.console.business.hotkey.entity.HotKey;
import org.lds.hotkey.console.business.hotkey.model.response.HotKeyBaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Objects;


@Component
public class HotKeyToBaseResponseConverter implements Converter<HotKey, HotKeyBaseResponse> {

    @Autowired
    private AppMapper appMapper;

    @Override
    public HotKeyBaseResponse convert(HotKey source) {
        App app = appMapper.selectById(source.getAppId());

        return HotKeyBaseResponse.builder()
            .id(source.getId())
            .appId(source.getAppId())
            .appName(Objects.isNull(app) ? null : app.getName())
            .key(source.getKey())
            .enable(source.getEnable())
            .duration(source.getDuration())
            .desc(source.getDesc())
            .creator(source.getCreator())
            .modifier(source.getModifier())
            .createTime(source.getCreateTime())
            .updateTime(source.getUpdateTime())
            .build();
    }

}


